package com.easyadmin.modules.sys.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.easyadmin.common.constant.SystemConstant;
import com.easyadmin.common.context.UserContext;
import com.easyadmin.modules.sys.domain.SysRole;
import com.easyadmin.modules.sys.service.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.easyadmin.common.web.domain.request.PageDomain;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.easyadmin.modules.sys.mapper.BCustomerInfoMapper;
import com.easyadmin.modules.sys.domain.BCustomerInfo;
import com.easyadmin.modules.sys.service.IBCustomerInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 客户信息Service业务层处理
 *
 * @author mars
 * @date 2025-02-14
 */
@Service
@AllArgsConstructor
public class BCustomerInfoServiceImpl extends ServiceImpl<BCustomerInfoMapper, BCustomerInfo> implements IBCustomerInfoService {


    private final SysUserService sysUserService;

    /**
     * 查询客户信息
     *
     * @param bCustomerInfo 客户信息
     * @param pageDomain
     * @return 客户信息 分页集合
     */
    @Override
    public PageInfo<BCustomerInfo> selectBCustomerInfoPage(BCustomerInfo bCustomerInfo, PageDomain pageDomain) {
        PageHelper.startPage(pageDomain.getPage(), pageDomain.getLimit());
        // 获取当前用户ID
        String userId = UserContext.currentUser().getUserId();
        // 查询角色
        List<SysRole> userRoles = sysUserService.getUserRole(userId);
        boolean b = userRoles.stream().anyMatch(x -> x.getRoleCode()
                .equals(SystemConstant.ADMIN_ROLE_CODE) && x.isChecked());
        // 非管理员设置用户ID
        if (!b) {
            SysRole sysRole = userRoles.stream().filter(SysRole::isChecked).collect(Collectors.toList()).get(0);
            String roleCode = sysRole.getRoleCode();
            if (SystemConstant.MANAGER.equals(roleCode)) {
                bCustomerInfo.setSourceSteamerId(userId);
            } else {
                bCustomerInfo.setUserId(userId);
            }
        }
        List<BCustomerInfo> data = baseMapper.selectBCustomerInfoList(bCustomerInfo);
        return new PageInfo<>(data);
    }

}
